package com.ecric.http.webService;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/9/22 10:30
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLKY  Co., Ltd. Inc. All rights reserved.
 */
public class WebServiceWeatherUtil {
    /**定义web Service 的命名空间*/
    public static final String SERVICE_NS = "http://webXml.com.cn";
    /**  定义web Service 提供服务的url*/
    public static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWs.asmx";

    /**
     *
     * 调用远程web  Service 获取省份列表
     *
     */
    public  static List<String> getProvinceList(){
        //调用的方法
        final String methodName = "getRegionProvince";
        final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
        ht.debug = true;
        //使用SOAP1.1 协议创建Envelop对象
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //实例化 SoapObject对象
        SoapObject soapObject  = new SoapObject(SERVICE_NS,methodName);
        envelope.bodyOut = soapObject;
        //设置与.NET提供web Service 保持较好的兼容性
        envelope.dotNet = true;
        FutureTask<List<String>> task = new FutureTask<List<String>>(new Callable<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                //调用web service
                ht.call(SERVICE_NS + methodName,envelope);
                if (envelope.getResponse() != null){
                    //获取服务器响应返回的soap消息
                    SoapObject result = (SoapObject) envelope.bodyIn;
                    SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
                    //解析服务器响应soap消息
                    return parseProvinceOrCity(detail);
                }
                return null;
            }
        });

        new Thread(task).start();
        try {
            return task.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     *
     * 根据省份获取城市列表
     *
     */
    public static List<String> getCityListByProvince(String province){
        final  String methodName = "getSupportCityString";
        final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
        ht.debug = true;
        SoapObject soapObject = new SoapObject(SERVICE_NS,methodName);
        soapObject.addProperty("theRegionCode",province);
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        FutureTask<List<String>> task = new FutureTask<List<String>>(new Callable<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                ht.call(SERVICE_NS + methodName,envelope);
                if (envelope.getResponse() != null){
                    SoapObject result = (SoapObject) envelope.bodyIn;
                    SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
                    return parseProvinceOrCity(detail);
                }
                return null;
            }
        });
        new Thread(task).start();
        try {
            return task.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return null;
    }

    private static List<String> parseProvinceOrCity(SoapObject detail){
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < detail.getPropertyCount(); i++) {
            result.add(detail.getProperty(i).toString().split(",")[0]);
        }
        return result;
    }


    public static SoapObject getWeatherByCity(String cityName){
        final String methodName = "getWeather";
        final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
        ht.debug = true;
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        SoapObject soapObject = new SoapObject(SERVICE_NS,methodName);
        soapObject.addProperty("thiCityCode",cityName);
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        FutureTask<SoapObject>  task = new FutureTask<SoapObject>(new Callable<SoapObject>() {
            @Override
            public SoapObject call() throws Exception {
                ht.call(SERVICE_NS + methodName, envelope);
                SoapObject result = (SoapObject) envelope.bodyIn;
                SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");
                return detail;
            }
        });

        new Thread(task).start();
        try {
            return task.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return null;
    }



}
